unit uniManCliente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uniManPadrao, DB, ADODB, Grids, DBGrids, StdCtrls, ExtCtrls,
  Buttons;

type
  TfrmManCliente = class(TfrmManPadrao)
    edtFantasia: TEdit;
    Label3: TLabel;
    lbl1: TLabel;
    edtCPFCNPJ: TEdit;
    lbl2: TLabel;
    edtMarca: TEdit;
    procedure btnPesquisaClick(Sender: TObject);
    procedure btnIncluirClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnEditarClick(Sender: TObject);
    procedure btnExcluirClick(Sender: TObject);
  private
    procedure p_Select(pParametros:String);
  public
    { Public declarations }
  end;

var
  frmManCliente: TfrmManCliente;

implementation

uses uniCadCliente, uniFuncBD, UniFuncoes, uniDtmSisnnovi;

{$R *.dfm}

{ TfrmManCliente }

procedure TfrmManCliente.p_Select(pParametros:String);
var sSQL : String;
begin
  sSQL := ' SELECT TB_CLIENTE.* FROM TB_CLIENTE '+
          ' LEFT JOIN TB_MARCA '+
          ' ON TB_CLIENTE.CD_CLIENTE = TB_MARCA.CD_CLIENTE '+
          ' WHERE 1=1';
  sSQL := sSQL + pParametros;
  sSQL := sSQL + ' ORDER BY RAZAO ';
  QR_Grid.Close;
  QR_Grid.SQL.Clear;
  QR_Grid.SQL.Add(sSQL);
  QR_Grid.Open;
end;

procedure TfrmManCliente.btnPesquisaClick(Sender: TObject);
var sParametros,
    sParametrosMarca : String;
begin
  inherited;
  if edtCodigo.Text <> '' then
    sParametros := sParametros + ' AND cd_cliente LIKE '+QuotedStr('%'+edtCodigo.Text+'%');

  if edtDescricao.Text <> '' then
    sParametros := sParametros + ' AND razao LIKE '+QuotedStr('%'+edtDescricao.Text+'%');

  if edtFantasia.Text <> '' then
    sParametros := sParametros + ' AND fantasia LIKE '+QuotedStr('%'+edtFantasia.Text+'%');

  if edtCPFCNPJ.Text <> '' then
    sParametros := sParametros + ' AND cnpj LIKE '+QuotedStr('%'+edtCPFCNPJ.Text+'%');

  if edtMarca.Text <> '' then
    sParametros := sParametros + ' AND descricao LIKE '+QuotedStr('%'+edtMarca.Text+'%');

  p_Select(sParametros);

end;

procedure TfrmManCliente.btnIncluirClick(Sender: TObject);
begin
  inherited;
  try
    Application.CreateForm(TfrmCadCliente, frmCadCliente);
    frmCadCliente.sEstado := 'I';
    frmCadCliente.sCdCampo := '';
    frmCadCliente.ShowModal;
   finally
     frmCadCliente.Free;
   end;
   p_Select('');
end;

procedure TfrmManCliente.FormShow(Sender: TObject);
begin
  inherited;
  p_Select('');
end;

procedure TfrmManCliente.btnEditarClick(Sender: TObject);
begin
  inherited;
  try
    Application.CreateForm(TfrmCadCliente, frmCadCliente);
    frmCadCliente.sEstado := 'E';
    frmCadCliente.sCdCampo := QR_Grid.FieldByName('CD_CLIENTE').AsString;
    frmCadCliente.ShowModal;
  finally
    frmCadCliente.Free;
  end;
  p_Select('');
end;

procedure TfrmManCliente.btnExcluirClick(Sender: TObject);
var sSQL : STring;
begin
  if not f_validaExcluirCliente(QR_Grid.FieldByName('CD_CLIENTE').AsString)then
    exit;
  //Deleta todas as marcas do cliente
  sSQL := 'DELETE FROM TB_MARCA WHERE CD_CLIENTE = '+QR_Grid.FieldByName('CD_CLIENTE').AsString;
  dtmSisnnovi.ExecuteQUERY(sSQL);
  
  inherited;

end;

end.
